<template>
  <div class="category">
    <div class="category__top" @scroll="onScroll">
      <div class="category__top__list" :style="{width:listW+'px'}">
        <div class="category__top__list__item" v-for="(item,index) in data" :key="index">
          <van-image class="category__top__list__item__img" :src="item.imageUrl"></van-image>
          <div class="category__top__list__item__text">{{item.name}}</div>
        </div>
        <!-- <category-item></category-item> -->
      </div>

    </div>
    <div class="category__bottom">
      <div class="category__bottom__bg">
        <div class="category__bottom__bg__progress" :style="{left:progressL+'px'}"></div>
      </div>
    </div>
  </div>
</template>

<script>
// import CategoryItem from './CategoryItem.vue';

export default {
  name: 'Category',
  // components: {
  //   CategoryItem,
  // },
  data() {
    return {
      data: [],
      listW: 0, // 一行宽度
      progresScrollW: 0, // 可滚动区域
      categoryScrollW: 0, // 金刚可滚动区域
      progressL: 0, // 进度条距离左侧偏移量
    };
  },
  methods: {
    onScroll(e) {
      const { scrollLeft } = e.target;
      let scrollPercent = +(scrollLeft / this.categoryScrollW).toFixed(2);
      if (scrollPercent > 1) {
        scrollPercent = 1;
      } else if (scrollPercent < 0) {
        scrollPercent = 0;
      }
      this.progressL = scrollPercent * this.progresScrollW;
    },
    calc() {
      this.$nextTick(() => {
        const length = this.data ? this.data.length : 0;
        const lineCount = Math.ceil(length / 2);
        const item = document.getElementsByClassName('category__top__list__item')[0];
        const itemW = item.getBoundingClientRect().width;
        const itemStyle = window.getComputedStyle(item);
        const marginLeft = +itemStyle.marginLeft.replace('px', '');
        const marginRight = +itemStyle.marginRight.replace('px', '');
        this.listW = (itemW + marginLeft + marginRight) * lineCount;
        const bg = document.getElementsByClassName('category__bottom__bg')[0];
        const progress = document.getElementsByClassName('category__bottom__bg__progress')[0];
        this.progresScrollW = progress.getBoundingClientRect().width;
        // 进度条可滚动区域
        this.scrollW = bg.getBoundingClientRect().width - progress.getBoundingClientRect().width;
        // 金刚视口宽度
        const category = document.getElementsByClassName('category__top')[0];
        this.categoryScrollW = this.listW - category.getBoundingClientRect().width;
        console.log('categoryScrollW', this.categoryScrollW);
      });
    },
  },
  watch: {
    '$store.state.homeList.category': {
      immediate: true,
      handler(val) {
        this.data = val;
        this.calc();
      },
    },
  },
};
</script>

<style lang="less" scoped>
.category {
  height: 294px;
  background: #ffffff;
  border-radius: 24px;
  margin: 0 18px;
  overflow: hidden;
  &__top {
    width: 100%;
    height: 265px;
    overflow-x: auto;
    overflow-y: hidden;
    &::-webkit-scrollbar {
      display: none;
    }
    &__list {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-direction: row;
      flex-wrap: wrap;
      &__item {
        width: 104px;
        height: 125px;
        margin: 0 21px;
        &__img {
          width: 88px;
          height: 88px;
        }
        &__text {
          width: 107px;
          height: 37px;
          font-size: 26px;
          color: #717171;
          letter-spacing: 0;
          text-align: center;
          font-weight: 400;
        }
      }
    }
  }
  &__bottom {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 29px;
    &__bg {
      position: relative;
      width: 86px;
      height: 6px;
      background: #d4d8dd;
      border-radius: 2px;
      &__progress {
        position: absolute;
        left: 0;
        top: 0;
        width: 43px;
        height: 6px;
        background: #ff6d29;
        border-radius: 2px;
      }
    }
  }
}
</style>
